import { hasDependency } from './runtime-utils.js';
import { Compiler } from './compiler.js';
import { logger } from '../../core/logger.js';
import { existsSync, cpSync } from 'node:fs';
import { rm } from 'node:fs/promises';
import path from 'node:path';

const PublicPath = path.join(process.cwd(), "public");
const PublicBuildPath = path.join(process.cwd(), ".robo", "public");
async function buildPublicDirectory() {
  try {
    if (await hasDependency("vite", true)) {
      await buildVite();
    } else {
      let time = Date.now();
      await rm(PublicBuildPath, { force: true, recursive: true });
      logger.debug("Public directory cleaned in", Date.now() - time, "ms");
      if (!existsSync(PublicPath)) {
        logger.debug("No public directory found. Skipping...");
        return;
      }
      time = Date.now();
      logger.debug("Copying public directory...");
      cpSync(PublicPath, PublicBuildPath, { force: true, recursive: true });
      logger.debug("Public directory copied in", Date.now() - time, "ms");
    }
  } catch (error) {
    logger.error("Failed to build Vite:", error);
  }
}
async function buildVite() {
  const time = Date.now();
  logger.debug("Building Vite...");
  const { build, loadConfigFromFile } = await import('vite');
  let config;
  const configEnv = {
    command: "build",
    isPreview: false,
    isSsrBuild: false,
    mode: "production"
  };
  const configPath = path.join(process.cwd(), "config", `vite.${Compiler.isTypescriptProject() ? "ts" : "mjs"}`);
  if (existsSync(configPath)) {
    config = (await loadConfigFromFile(configEnv, configPath))?.config;
  } else if (existsSync(path.join(process.cwd(), `vite.config.${Compiler.isTypescriptProject() ? "ts" : "js"}`))) {
    config = (await loadConfigFromFile(configEnv))?.config;
  } else {
    logger.debug("No Vite config found. Skipping...");
    return;
  }
  logger.debug("Vite config loaded:", config);
  await build({
    logLevel: "warn",
    ...config ?? {},
    build: {
      ...config?.build ?? {},
      emptyOutDir: true,
      outDir: path.join(".robo", "public")
    }
  });
  logger.debug("Vite build completed in", Date.now() - time, "ms");
}

export { buildPublicDirectory, buildVite };
